module.exports = {
	getAccountTx: getAccountTx,
	getAccountTxList: getAccountTxList,
	getAccount: getAccount,
	getAccountTotal: getAccountTotal,
	getAccountOneTx: getAccountOneTx,
	getPayReq: getPayReq,
	getPayReqById: getPayReqById,
	pay: pay,
	updateTx: updateTx,
	updateAccount: updateAccount
};
var db = require('../common/database/db');
var moment = require('moment');

function updateAccount(account, cb) {
	db.queryWithParam('update memberaccount set balance=? where id=?', [account.balance, account.id], cb)
}
function updateTx(tx, cb) {
	db.queryWithParam('INSERT INTO macct_tx (id,amount,balance,createTime,store,txType,memberAccount_id,orderNo,subChannel,txTime) VALUES(?,?,?,?,?,?,?,?,?,?)',
		[tx.id, tx.amount, tx.balance, tx.createTime, tx.store, tx.txType, tx.memberAccount, tx.orderNo, tx.subChannel, tx.txTime], cb)
}
function pay(data, cb) {
	db.queryWithParam('update payment_request set status=? , oper_token=? where id=?', [data.status, Number(data.oper_token), data.id], cb)
}
function getPayReqById(id, cb) {
	var date = moment();
	db.queryWithParamSingle('select * from payment_request where id=? and status=?  and expired_time between ? and ? order by expired_time desc limit 1',
		[id, 'opened', date.format("YYYY-MM-DD HH:mm:ss"), date.add('minute', 6).format("YYYY-MM-DD HH:mm:ss")], cb);
}
function getPayReq(memberId, storeId, cb) {
	var date = moment();
	db.queryWithParamSingle('select * from payment_request where store_id=? and payer_id=? and status=? and expired_time between ? and ? and payer_type=? order by expired_time desc limit 1',
		[storeId, memberId, "opened", date.format("YYYY-MM-DD HH:mm:ss"), date.add('minute', 6).format("YYYY-MM-DD HH:mm:ss"), 'member'], cb);
}
function getAccountTx(memberId, cb) {
	db.queryWithParamSingle('SELECT COUNT(id) as qty,SUM(amount) as amountTotal FROM macct_tx WHERE memberAccount_id=? AND txType IN (?)',
		[memberId, ['pay', 'refund', 'deposit', 'manual-update']], cb);
}
function getAccountTotal(memberId, types, cb) {
	db.queryWithParamSingle('SELECT COUNT(id) as qty,SUM(amount) as amountTotal FROM macct_tx WHERE memberAccount_id=? AND txType IN (?)',
		[memberId, types], cb);
}

function getAccountTxList(memberId, types, startDateTs, endDateTs, cb) {
	var startDate = moment(startDateTs).format("YYYY-MM-DD HH:mm:ss");
	var endDate = moment(endDateTs).format("YYYY-MM-DD HH:mm:ss");
	db.queryWithParam('SELECT * FROM macct_tx WHERE memberAccount_id=? and txType in (?) and createTime between ? and ? order by createTime desc',
		[memberId, types, startDate, endDate], function (err, txList) {
			if (!err) {
				for (var i in txList) {
					txJson(txList[i])
				}
			}
			cb(err, txList);
		});
}

function txJson(tx) {
	tx.creator = JSON.parse(tx.creator);
	tx.store = JSON.parse(tx.store);
	tx.terminal = JSON.parse(tx.terminal);
}
function getAccount(memberId, cb) {
	db.queryWithParamSingle("select * from memberaccount where id=?", [memberId], cb);
}

function getAccountTxByMonth(memberId, types, startDate, endDate, cb) {
	db.queryWithParamSingle('SELECT count(amount) as totalAmount FROM macct_tx WHERE memberAccount_id=? and txType in (?) and createTime between ? and ? order by createTime desc',
		[memberId, types, startDate, endDate], cb);
}

function getAccountOneTx(id, cb) {
	db.queryWithParamSingle("select * from macct_tx where id=?", [id], function (err, tx) {
		if (!err) {
			txJson(tx);
		}
		cb(err, tx)
	});
}

